package com.snap.awesomeserial.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.snap.awesomeserial.R;
import com.snap.awesomeserial.adapter.StepAdapter;
import com.snap.awesomeserial.base.BaseActivity;
import com.snap.awesomeserial.bean.NAStep;
import com.snap.awesomeserial.bean.TemplateInfo;
import com.snap.awesomeserial.framework.MoleExecutor;
import com.snap.awesomeserial.framework.MoleTask;
import com.snap.awesomeserial.model.DBManager;
import com.snap.awesomeserial.model.FileManager;
import com.snap.awesomeserial.ui.widget.DialogView;
import com.snap.awesomeserial.ui.widget.IconButton;
import com.snap.awesomeserial.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DemoActivity extends BaseActivity {

    private static final String TAG = "TemplateActivity";
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.start_btn)
    IconButton startBtn;
    @BindView(R.id.steps_rv)
    RecyclerView stepsRv;
    private int currentCheckedId;
    private String fileName;
    private TemplateInfo templateInfo;
    private StepAdapter stepAdapter;

    @Override
    protected int layoutResId() {
        return R.layout.activity_demo;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            currentCheckedId = savedInstanceState.getInt("currentCheckId");
        }
        fileName = getIntent().getStringExtra("fileName");
        titleTv.setText(fileName);
        if (TextUtils.isEmpty(fileName)) {
            Logger.e("查看实验详情失败！实验文件为空");
            return;
        }

        getDataFromFile();
    }


    private void getDataFromFile() {
        MoleExecutor.getInstance().execute(new MoleTask<TemplateInfo>() {
            @Override
            public TemplateInfo runInBackground() {
                return FileManager.getInstance().getTempDataByName(fileName);
            }

            @Override
            public void onCompleted(TemplateInfo result) {
                templateInfo = result;
                //初始化选择
                for (NAStep step : templateInfo.getNAProgram()) {
                    step.setChoose(step.isChecked());
                }
                LinearLayoutManager manager = new LinearLayoutManager(DemoActivity.this);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                stepsRv.setLayoutManager(manager);
                stepAdapter = new StepAdapter(DemoActivity.this);
                stepsRv.setAdapter(stepAdapter);
                stepAdapter.bindDataList(templateInfo.getNAProgram());
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("currentCheckId", currentCheckedId);
        super.onSaveInstanceState(outState);
    }

    @OnClick({R.id.back_btn, R.id.delete_btn, R.id.edit_btn, R.id.start_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.delete_btn:
                new DialogView(this)
                        .setTitleContent("确认删除实验项目")
                        .setOnClickListener("删除", new DialogView.OnWarnListener() {
                            @Override
                            public void onWarnClick() {
                                DBManager.get().deleteDemo(i -> {
                                    if (i > 0) {
                                        FileManager.getInstance().removeTempData(fileName);
                                        ToastUtils.get().showText("删除实验项目成功");
                                        DemoActivity.this.finish();
                                    }
                                }, fileName);
                            }
                        }).show();
                break;
            case R.id.edit_btn:
                Intent intent = new Intent();
                intent.putExtra("templateInfo", templateInfo);
                intent.setClass(this, DemoEditActivity.class);
                startActivity(intent);
                break;
            case R.id.start_btn:
                if (!stepAdapter.hasChecked()) {
                    ToastUtils.get().showText("请选择操作步骤");
                    return;
                }
                new DialogView(this)
                        .setTitleContent("请预先确认各项参数设置")
                        .setOnClickListener("开始", new DialogView.OnSureListener() {
                            @Override
                            public void onSureClick() {
                                Intent intent1 = new Intent();
                                intent1.putExtra("templateInfo", templateInfo);
                                intent1.setClass(DemoActivity.this, RunningActivity.class);
                                startActivity(intent1);
                            }
                        }).show();
                break;
            default:
        }
    }
}

